package com.gmzg.gmzgnews.news.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.news.entries.HeadNews;
import com.gmzg.gmzgnews.news.enums.NewsType;
import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

import static android.content.ContentValues.TAG;

/**
 * Created by Jasen on 2016 2016/11/1.15:29
 * Email:1142803753@qq.com
 * weixin: aserbao
 */

public class NewsHeadAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private static final int NEWS_VIEWPAGER = 0;
    private static final int NEWS_OTHER = 1;
    private NewsType message;
    private Context mContext;
    private List<HeadNews> mHeadNewsList;
    private OnChildClickListener mChildClickListener;
    private RecyclerView mRecyclerView;
    private View mHeadView;
    private int mRetPositon;

    public void setOnChildClickListener(OnChildClickListener childClickListener) {
        mChildClickListener = childClickListener;
    }

    /**
     * 当连接到RecycleView的时候，传递数据的时候使用的方法
     * @param recyclerView
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.mRecyclerView = null;
    }

    public NewsHeadAdapter( Context context, List<HeadNews> headNewsList,NewsType message) {
        this.message = message;
        mContext = context;
        mHeadNewsList = headNewsList;
    }

    public void addAll(Collection<? extends  HeadNews> collection){
        mHeadNewsList.addAll(collection);
        notifyDataSetChanged();
    }
    @Override
    public int getItemViewType(int position) {
        mRetPositon = 1;
        if (mHeadNewsList != null && mHeadView != null) {
            if (position == 0){
                mRetPositon = NEWS_VIEWPAGER;
            }else{
                mRetPositon = NEWS_OTHER;
            }
        }
        return mRetPositon;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = null;
        RecyclerView.ViewHolder ret = null;
        Log.d(TAG, "onCreateViewHolder() returned: "  );
        switch (viewType) {
            case NEWS_VIEWPAGER:
                HeadViewHolder headViewHolder = new HeadViewHolder(mHeadView);
                ret = headViewHolder;
            case NEWS_OTHER:
                itemView = LayoutInflater.from(mContext)
                        .inflate(R.layout.news_head_item, parent, false);
                ret=new MyViewHolder(itemView);
                break;
        }
        itemView.setOnClickListener(this);
        return ret;
    }

    /**
     * 用来接收用户传入的自定义View
     */
    public class HeadViewHolder extends RecyclerView.ViewHolder {
        public HeadViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * 用来传递用户定义的view
     * @param headView
     */
    public void addHeadView(View headView) {
        mHeadView = headView;
    }
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        Log.d(TAG, "onBindViewHolder() returned: " );
        MyViewHolder holder = null;
        if (h instanceof MyViewHolder){
             holder = (MyViewHolder)h;
        }
        switch (mRetPositon) {
            case NEWS_OTHER:
                Picasso.with(mContext)
                        .load("http://tnfs.tngou.net/img"+ mHeadNewsList.get(position).getImg())
                        .into(holder.mNewsHeadItemImage);
                holder.setNewsHeadItemTitle(position);
                holder.setNewsHeadItemType(position);
                break;
            case NEWS_VIEWPAGER:

                break;
        }

    }

    @Override
    public int getItemCount() {
        return mHeadNewsList.size();
    }

    @Override
    public void onClick(View v) {
        switch (mRetPositon) {
            case NEWS_OTHER:
                if (mRecyclerView != null & mChildClickListener != null) {
                    int position = mRecyclerView.getChildAdapterPosition(v);
                    mChildClickListener.onChildClick(mRecyclerView, v, position, mHeadNewsList.get(position));
                }
                break;
            case NEWS_VIEWPAGER:
                Toast.makeText(mContext, "我被点击了", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView mNewsHeadItemImage;
        private TextView mNewsHeadItemTitle;
        private TextView mNewsHeadItemType;
        public MyViewHolder(View itemView) {
            super(itemView);
            mNewsHeadItemImage = (ImageView) itemView.findViewById(R.id.news_head_item_image);
            mNewsHeadItemTitle = (TextView) itemView.findViewById(R.id.news_head_item_title);
            mNewsHeadItemType = (TextView) itemView.findViewById(R.id.news_head_item_type);
        }

        public void setNewsHeadItemTitle(int position) {
            if (mHeadNewsList != null) {
            mNewsHeadItemTitle.setText(mHeadNewsList.get(position).getTitle());
            }
        }

        public void setNewsHeadItemType(int position) {
            if (mHeadNewsList != null) {
            mNewsHeadItemType.setText(mHeadNewsList.get(position).getFromname());
            }
        }
    }
    public interface OnChildClickListener{
        void onChildClick(RecyclerView parent, View view, int position, HeadNews data);
    }
}

